/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { message } from 'antd';
import { setAuthority } from '@/utils/authority';
import { reloadAuthorized } from '@/utils/Authorized';
import { getCookie } from '@/utils/utils';
import { setCompanyInfo, getCompanyInfo, getBrandInfo } from '../services/user';

const noop = () => {};
export default {
  namespace: 'info',
  state: {
    status: 'loading',
    companyData: null,
    brandLists: null,
  },
  effects: {
    // 注册Assignment to function parameter 'payload'
    *submit({ payload, then = noop }, { call, select }) {
      const { companyData } = yield select(_ => _.info);
      let porps = {};
      if (companyData) {
        const { id } = companyData;
        porps = { ...payload, id };
      } else {
        porps = { ...payload };
      }
      const companyDatas = { ...porps };
      const response = yield call(setCompanyInfo, companyDatas);
      if (!response) return;
      const { code, data, msg } = response;
      if (code == 0 && data) {
        message.success('更新成功', 0.5, () => {
          then(payload);
        });
      } else {
        message.error(msg);
      }
    },
    *getInfo({ payload, then = () => {} }, { call, put, select }) {
      const quickLoginType = Number(getCookie('5376912e6b8535b3ee565675312e8968') || 0);
      let { companyData } = yield select(_ => _.info);
      let status = 'show';
      if (!payload || payload.source != 'correct') {
        if (companyData) {
          yield put({
            type: 'save',
            payload: {
              status,
            },
          });
          return;
        }
      }
      const response = yield call(getCompanyInfo, { ...payload });
      if (!response) return;
      const { code, data } = response;
      if (code == 0 && data) {
        companyData = data;
        const {
          brand_name,
          shop_id,
          roles,
          pid,
          phone,
          parent_phone,
          user_info = {},
          sub_privilege,
          is_special,
          disclaimer_status,
          id,
          area_ids,
          gp_area_ids,
          inn_area_ids,
          is_sitescan,
        } = data;
        const currentUser = {
          shop_id,
          phone,
          parent_phone,
          name: brand_name,
          roles,
          sub_privilege,
          pid,
          user_info: {
            area_ids,
            gp_area_ids: gp_area_ids || [],
            inn_area_ids: inn_area_ids || [],
            id,
            ...user_info,
          },
          is_special: is_special == 1, // 是否是特许加盟商
          is_sitescan, // 是否开启场地扫码权限
          disclaimer_status, // 同意使用共配，1： 是， 0：否
          id,
          quickLoginType, // 快捷登录类型，控制代入库，不存在或者0 不展示   1 仅展示查询   2 查询和新增查看详情  3查询新增和导入
        };
        yield put({
          type: 'user/saveCurrentUser',
          payload: {
            currentUser,
          },
        });
        setAuthority({
          isRoot: pid == 0,
          root: roles,
          sub: sub_privilege.filter(item => item !== '*'),
        });
        reloadAuthorized();
        then(currentUser);
        if (!brand_name || !phone) {
          status = 'editor';
        } else {
          status = 'show';
        }
      } else {
        status = 'editor';
        companyData = null;
      }
      yield put({
        type: 'save',
        payload: {
          status,
          companyData,
        },
      });
    },
    *switchStatus({ payload }, { put }) {
      yield put({
        type: 'save',
        payload: {
          status: payload.status,
        },
      });
    },
    *getBrandInfo({ payload }, { call, put }) {
      const response = yield call(getBrandInfo, { ...payload });
      if (!response) return;
      const { code } = response;
      if (code == 0 && response.data) {
        yield put({
          type: 'save',
          payload: {
            brandLists: response.data,
          },
        });
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
